Page({
	data: {
		nickName: 'name',
		showModalInput: false,
		newUserName: '',
		inputShowed: true,
		avatarUrl: '/icon/225默认头像.png',
		isChangingName:false,
		isChangingPhoto:false,
		newnickName:'',
		newavatarUrl:''
	},
	chooseImage: function (event) {
    let that = this;
    wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album'],
        success: function (res) {
            const tempFiles = res.tempFiles;
            that.setData({
                avatarUrl: tempFiles[0].tempFilePath,
								isChangingPhoto:true,
								newavatarUrl:tempFiles[0].tempFilePath
            });
						console.log(tempFiles[0].tempFilePath);	
				}		
		})
  },
	onLoad() {
    let avatarUrl = wx.getStorageSync('avatarUrl');
		let nickName = wx.getStorageSync('SetnickName');
    if (avatarUrl) {
        this.setData({
            avatarUrl: avatarUrl
        });
    }
		if (nickName) {
        this.setData({
						nickName:nickName
        });
    }
		wx.request({
    url: 'http://localhost:8001/user/setUser',
    method: 'POST',
    success: (res) => {  
        console.log('请求成功');
        this.setData({
            nickname: res.data.userName,
            avatarUrl: res.data.photo
        });
    },
    fail: (err) => { 
        console.error('请求失败', err);
    }
  });
    wx.setStorageSync('avatarUrl', this.data.newavatarUrl);
		wx.setStorageSync('SetnickName', this.data.newnickName);
	},
	showInputBox: function () {
		this.setData({
			showModalInput: true,
			newUserName: this.data.nickName
		});
	},
	confirmInput: function (res) {
		let newName = this.data.newUserName;
		console.log(newName)
		if (newName === '') {
			wx.showToast({
				title: '用户名不能为空',
				icon: 'none',
				duration: 2000
			});
			return;
		};
		this.setData({
			nickName: newName,
			showModalInput: false,
			isChangingName:true,
			newnickName:newName
		});
		console.log(this.data.nickName)
	},

	cancelInput: function () {
		console.log('用户点击了取消输入');
		this.setData({
			showModalInput: false
		});
	},

	inputChange: function (e) {
		this.setData({
			newUserName: e.detail.value
		});
	},
	onSubmit(event) {
		const	{nickName} = event.detail.value
		this.setData({
			nickName:nickName
		})
	},
	upDateChanges: function(){
		const N = this.data.nickName
		const P = this.data.avatarUrl
		if ((!this.data.isChangingPhoto)&&(!this.data.isChangingName)) {
			wx.showToast({
				title: '未进行任何修改',
				icon:'error',
				duration:2000
			})
			return;
		}
		wx.request({
      url: 'http://localhost:8001/user/setUser',
      method: 'POST',
      data: {
        username: N,
				photo:P
      },
      success: function(res) {
        console.log('修改成功');
				console.log(N);
      },
      fail: function(err) {
        console.error('修改失败', err);
				console.log(N);
				console.log(P);
      }
    });
		wx.setStorageSync('avatarUrl', this.data.newavatarUrl);
		wx.setStorageSync('SetnickName', this.data.newnickName);
		console.log(this.data.newnickName+1);
		console.log(this.data.newavatarUrl);
		this.setData({
			isChangingPhoto: false,
			isChangingName:false
		});
		wx.showToast({
				title: '修改完成',
				duration:2000
			})
	}
})